<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sell extends MY_Controller {
    /**
     * @var ConsumerOrderModel
     */
    public $mOrder = null;

	public function __construct(){
		parent::__construct();
        $this->loadModel('ConsumerOrderModel', 'mOrder', 'consumer');
	}

    /**
     * 首页
     */
    public function index(){
        $timeGap = trim(post('timegap'));
        if (!$timeGap){
            // 最近10天
            $startTime = strtotime("-10 day");
            $endTime = 0;
        }else{
            $timeGapArr = explode(' - ', $timeGap);
            $startTime = strtotime($timeGapArr[0]);
            $endTime = strtotime($timeGapArr[1]);
            $dayA = ceil(($endTime-$startTime)/86400);
            if ($dayA > 100){
                $endTime = strtotime("+3 month",$startTime);
            }
        }
        $data = $this->mOrder->getSalesStatistics($startTime, $endTime, $this->countyItem);
        $result = array(
            'order' => array(),
            'amount' => array(),
            'sign' => array(),
            'time' => array()
        );
        foreach ($data as $k => $v){
            $result['order'][] = $v['order_num'];
            $result['amount'][] = $v['amount'];
            $result['sign'][] = $v['sign'];
            $result['time'][] = $v['time'];
        }
        // 统计当天销售情况
        $sameDayStarTime = strtotime(date('Y-m-d', time()));
        $sameDay = $this->mOrder->getSalesStatistics($sameDayStarTime, 0, $this->countyItem);
        $endTime = $endTime == 0 ? time() : $endTime;
        if (empty($sameDay)){
            $sameDay = array(
                'amount' => 0,
                'order_num' => 0,
                'time' => 0,
                'sign' => 0,
            );
        }else{
            $sameDay = $sameDay[0];
        }
        $data = array(
            'result' => json_encode($result),
            'list' => $data,
            'timeGap' => $timeGap,
            'sameDay' => $sameDay,
            'startTime' => dateFormat($startTime),
            'endTime' => dateFormat($endTime),
            'cancelOrder' => $this->mOrder->getTotalOrder($sameDayStarTime, true), // 今天取消订单数量
        );
        $this->view('statistics/sell/index', $data, 'datatable,date_range_picker,e_chart');
    }

    /**
     * 订单详情
     */
    public function saleList(){
        //表单验证
        $vConf = array(
            array('time', '当天时间', 'required'),
        );
        $this->formDataValidation($vConf, array('time' => get('time')));
        $time = trim(get('time'));
        $data = array(
            'status' => array(
                '4'=>'待评价',
                '5'=>'已完成',
            ),
            'expenseType' => $this->mOrder->expenseType(),
            'payType' => $this->mOrder->payType(),
            'time' => $time,
            'title' => '订单详情'
        );
        $this->view('statistics/sell/saleList', $data, 'datatable');
    }

    /**
     * 获取订单详情列表
     */
    public function getSaleList(){
        $data = $this->mOrder->adminPcGetOrderList();
        foreach ($data['data'] as $k=>$v){
            $data['aaData'][$k][] = $v['code'];
            $data['aaData'][$k][] = $v['merchant'];
            $data['aaData'][$k][] = $v['consumer'];
            $data['aaData'][$k][] = $this->mOrder->expenseType($v['expense_type']);
            $data['aaData'][$k][] = $this->mOrder->payType($v['pay_type']);
            $data['aaData'][$k][] = $v['total_amount'];
            $data['aaData'][$k][] = dateFormat($v['addtime']);
            if ($v['expense_type'] == 2){
                $data['aaData'][$k][] = $this->mOrder->storeStatus($v['status'], true);
            }else{
                $data['aaData'][$k][] = $this->mOrder->status($v['status'], true);
            }
            $data['aaData'][$k][] = adminCreateBtn(array(
                'label' => '查看',
                'attr' => array(
                    'href' => getUrl('order/home/details?id='.$v['id']),
                    'class' => 'btn green-stripe btn-xs default'
                ),
                'tag' => 'a',
                'permissions' => array(
                    'module' => 'classes',
                    'key' => 'edit'
                )
            ));
        }
        unset($data['data']);
        echo json_encode($data);

    }

}
